package com.zhb.cloud.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zhb.cloud.core.JsonReturn;
import com.zhb.cloud.iservice.IUserService;
import com.zhb.cloud.model.User;
import com.zhb.cloud.service.UserService;

import org.springframework.stereotype.Service;

/**
 * @ClassName: MyCityServiceImpl
 * @author: zhb
 * @date: 2017年8月8日 下午2:57:59
 * @Description: 实际服务的发起
 *
 */
@Service
public class UserServiceImpl implements IUserService {
	@Reference(version = "1.0.0")
	private UserService userService;

	/**
	 * @Title: findByUserName
	 * @author: zhb
	 * @date: 2017年8月9日 下午4:25:01
	 * @Description: 使用注解做熔断处理--实际做法(创建hystrixCommand来进行操作)
	 * @param userName
	 * @return
	 */
	@Override
	@HystrixCommand(groupKey = "user", commandKey = "findUser", fallbackMethod = "findByUserNameFallBack", threadPoolProperties = {
			@HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "100"),
			@HystrixProperty(name = "queueSizeRejectionThreshold", value = "30") }, commandProperties = {
					@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
					@HystrixProperty(name = "execution.timeout.enabled", value = "true"),
					@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") })
	public JsonReturn findByUserName(String userName) throws Exception {
		Thread.sleep(6000);
		User user = userService.findByUserName(userName);
		JsonReturn jsonReturn = new JsonReturn();
		jsonReturn.setCode(200);
		jsonReturn.setData(user);
		jsonReturn.setMessage("查询成功");
		return jsonReturn;
	}

	public JsonReturn findByUserNameFallBack(String userName) {
		JsonReturn jsonReturn = new JsonReturn();
		jsonReturn.setCode(100);
		jsonReturn.setData("查询失败");
		jsonReturn.setMessage("活动太火爆");
		return jsonReturn;
	}
}
